import os
import sys
base_dir = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
sys.path.append(base_dir)
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "webservices.settings")
os.environ['DJANGO_SETTINGS_MODULE'] = 'webservices.settings'
from common.mg_core.mg import MONGODB
from django.conf import settings
from common.mg_core.mg import insert_or_update
from common.es_core.report_creator import ReportCreator
from event_rule.dwc.op_event_rule.user_analysis import USER_ANALYSIS_CONFIG
from event_rule.dwc.op_event_rule.withdraw_analysis import WITHDRAW_CONFIG

for index in settings.DWC_INDEX:
    collection = '_'.join(['op', 'user', 'report'])
    report = MONGODB[index][collection].find({"day": {"$gt": "2019-07-01"}, "active_bind_user": {"$gt": 0}})
    total_count = MONGODB[index][collection].count({})
    for idx, each in enumerate(report):
        each = ReportCreator.reprocess_value_expr(each, USER_ANALYSIS_CONFIG, index)
        insert_or_update(each, index, collection)
        if idx % 1000 == 0:
            print(index, idx, total_count)

    collection = '_'.join(['op', 'withdraw', 'report'])
    report = MONGODB[index][collection].find({"day": {"$gte": "2019-08-01"}})
    total_count = MONGODB[index][collection].count({})
    for idx, each in enumerate(report):
        each = ReportCreator.reprocess_value_expr(each, WITHDRAW_CONFIG, index)
        insert_or_update(each, index, collection)
        if idx % 1000 == 0:
            print(index, idx, total_count)